﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    class Print
    {
        #region dll
        [DllImport("EZio64.dll")]
        public static extern void setup(int a, int b, int c, int d, int e, int f);

        [DllImport("EZio64.dll")]
        public static extern void setbaudrate(int n);

        // n = 0 → LPT1 
        // n = 1 → COM1 
        // n = 2 → COM2 
        // n = 3 → COM3 
        // n = 4 → COM4 
        // n = 5 → LPT2 
        // n = 6 → USB         
        [DllImport("EZio64.dll")]
        public static extern void openport(
            [MarshalAs(UnmanagedType.LPStr)]
            string n);

        [DllImport("EZio64.dll")]
        public static extern void closeport();


        [DllImport("EZio64.dll")]
        public static extern void sendcommand(
            [MarshalAs(UnmanagedType.LPArray)]
            byte[] command);

        [DllImport("EZio64.dll")]
        public static extern void sendcommand(
            [MarshalAs(UnmanagedType.LPStr)]
            string command);


        [DllImport("EZio64.dll")]
        public static extern void extloadimage(
            [MarshalAs(UnmanagedType.LPStr)]
            string filename,
            [MarshalAs(UnmanagedType.LPStr)]
            string image_name,
            [MarshalAs(UnmanagedType.LPStr)]
            string image_type);

        // 1 -> Success, 0 -> Fail
        [DllImport("EZio64.dll")]
        public static extern int ecTextOut(int x, int y, int b,
            [MarshalAs(UnmanagedType.LPStr)]
            string c,
            [MarshalAs(UnmanagedType.LPStr)]
            string d);

        // 1 -> Success, 0 -> Fail
        [DllImport("EZio64.dll")]
        public static extern int ecTextOutR(int x, int y, int b,
            [MarshalAs(UnmanagedType.LPStr)]
            string c,
            [MarshalAs(UnmanagedType.LPStr)]
            string d, int e, int f, int g);

        // 1 -> Success, 0 -> Fail
        [DllImport("EZio64.dll")]
        public static extern int ecTextDownLoad(int b, string c, string d, int e, int f, int g,
            [MarshalAs(UnmanagedType.LPStr)]
            string name);

        // 1 -> Success, 0 -> Fail
        [DllImport("EZio64.dll")]
        public static extern int putimage(int x, int y,
            [MarshalAs(UnmanagedType.LPStr)]
            string filename, int degree);

        // 1 -> Success, 0 -> Fail
        [DllImport("EZio64.dll")]
        public static extern int downloadimage(
            [MarshalAs(UnmanagedType.LPStr)]
            string filename, int degree,
            [MarshalAs(UnmanagedType.LPStr)]
            string name);
        #endregion

        /// <summary>
        /// 是否打印文字
        /// </summary>
        private bool isPrintText = true;

        /// <summary>
        /// 每列打印标签个数
        /// </summary>
        public int column = 3;

        /// <summary>
        /// 标签之间间隔
        /// </summary>
        public int width = 216;

        /// <summary>
        /// 构造函数
        /// </summary>
        public Print()
        {
        }

        /// <summary>
        /// 给打印机发送命令
        /// </summary>
        /// <param name="cmd">命令</param>
        private void SendCommand(string cmd)
        {
            openport("6"); //Ex:USB
            try
            {
                sendcommand(cmd);
                closeport();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
                return;
            }
        }

        #region 打印机设定
        /// <summary>
        /// 标签纸类型 
        /// </summary>
        public bool paper = true;

        /// <summary>
        /// 标签长度
        /// </summary>
        public int labelLength = 25;

        /// <summary>
        /// 区间长度
        /// </summary>
        public int gapLength = 2;

        /// <summary>
        /// 黑线距标之间的距离
        /// </summary>
        public int z = 0;

        /// <summary>
        /// 标签的宽度
        /// </summary>
        public int labelWidth = 80;

        /// <summary>
        /// 列印黑度
        /// </summary>
        public int dark = 10;

        /// <summary>
        /// 列印张数
        /// </summary>
        public int copyNO = 1;

        /// <summary>
        /// 列印速度
        /// </summary>
        public int speed = 4;

        /// <summary>
        /// 列印模式 "^AD":热感模式，"^AT":热传模式
        /// </summary>
        public string mode = "^AD";

        /// <summary>
        /// 每张标签复印张数
        /// </summary>
        public int pageNO = 1;

        /// <summary>
        /// 左边界起印点
        /// </summary>
        public int left = 0;

        /// <summary>
        /// 上边界起印点
        /// </summary>
        public int top = 0;

        /// <summary>
        /// 自动剥纸机/贴标机设定 0：关闭 1：剥纸机打开/贴标机关闭 2：剥纸机关闭/贴标机打开 
        /// </summary>
        public int machine = 0;

        /// <summary>
        /// 每几张标签裁切一次 0：关闭 ，1~32767： 数目
        /// </summary>
        public int cutCount = 0;

        /// <summary>
        /// 停歇点
        /// </summary>
        public int stopPoint = 18;

        /// <summary>
        /// 反向列印  
        /// </summary>
        public int reDirection = 200;

        /// <summary>
        /// 整页旋转列印  0:旋转0 ,1:旋转90,2:旋转180,3:旋转270
        /// </summary>
        public int allRotationPrint = 0;

        /// <summary>
        /// 标签起始符号 ^L:正常  ^LI：反白 ^LM：镜像 ^LRn：旋转
        /// </summary>
        public string sign = "^L";
        #endregion 

        #region  二维码设定
        /// <summary>
        /// 条码左上角水平位置
        /// </summary>
        public int QRposX = 30;

        /// <summary>
        /// 条码左上角垂直位置
        /// </summary>
        public int QRposY = 30;

        /// <summary>
        /// 条码模式
        /// </summary>
        public int QRmode = 5;

        /// <summary>
        /// 条码类型
        /// </summary>
        public int QRtype = 2;

        /// <summary>
        /// 条码错误修正等级(L,M,Q,H)
        /// </summary>
        public string QRerr = "M";

        /// <summary>
        /// 条码遮罩因子
        /// </summary>
        public int QRmask = 8;

        /// <summary>
        /// 条码放大倍数(1~8)
        /// </summary>
        public int QRmul = 5;

        /// <summary>
        /// 条码内容长度（包含换行）
        /// </summary>
        public int QRlength = 12;

        /// <summary>
        /// 条码旋转角度(0,1,2,3)
        /// </summary>
        public int QRroatae = 0;

        /// <summary>
        /// 条码资料内容
        /// </summary>
        public string QRdata = "123456789012";
        #endregion

        #region 文字设定
        /// <summary>
        /// 文字左上角水平位置
        /// </summary>
        public int TextPosX = 121;

        /// <summary>
        /// 文字左上角垂直位置
        /// </summary>
        public int TextPosY = 31;

        /// <summary>
        /// 文字水平放大倍数
        /// </summary>
        public int TextXMul = 1;

        /// <summary>
        /// 文字垂直放大倍数
        /// </summary>
        public int TextYMul = 1;

        /// <summary>
        /// 文字旋转
        /// </summary>
        public int TextRotaion = 0;

        /// <summary>
        /// 文字间距
        /// </summary>
        public int TextGap = 0;

        /// <summary>
        /// 文字资料内容
        /// </summary>
        public string TextData = "123456";
        #endregion

        /// <summary>
        /// 获取打印机配置命令
        /// </summary>
        /// <returns></returns>
        private string GetPrintConfigCmd()
        {
            string cmd = string.Empty;
            string enter = "\r\n";
            if (paper)
            {
                cmd += "^Q" + this.labelLength + "," + this.gapLength + enter;
            }
            else
            {
                string strZ = z > 0 ? z.ToString() + "=" : (0 - z).ToString() + "-";
                cmd += "^Q" + this.labelLength + "," + this.gapLength + "," + strZ + enter;
            }

            cmd += "^W" + this.labelWidth + enter;
            cmd += "^H" + this.dark + enter;
            cmd += "^P" + this.copyNO + enter;
            cmd += "^S" + this.speed + enter;
            cmd += this.mode + enter;
            cmd += "^C" + this.pageNO + enter;
            cmd += "^R" + this.left + enter;
            if (top >= 0)
            {
                cmd += "~Q" + "+" + this.top + enter;
            }
            else
            {
                cmd += "~Q" + this.top + enter;
            }
            cmd += "^O" + this.machine + enter;
            cmd += "^D" + this.cutCount + enter;
            cmd += "^E" + this.stopPoint + enter;
            cmd += "~R" + this.reDirection + enter;
            cmd += "^XSET,ROTATION," + this.allRotationPrint + enter;
            cmd += this.sign + enter;
            return cmd;
        }

        /// <summary>
        /// 获取二维码
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private string GetQRConfigCmd(int index)
        {
            string cmd = string.Empty;
            cmd = "W" + (QRposX + index * width).ToString() + "," + QRposY + "," + QRmode + "," + QRtype + "," + QRerr + "," + QRmask + "," + QRmul + "," + QRlength + "," + QRroatae + "\n" + QRdata + "\r\n";
            return cmd;
        }

        /// <summary>
        /// 获取文字
        /// </summary>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        private string GetTextConfigCmd(string data, int index)
        {
            TextData = data;
            string cmd = string.Empty;
            cmd = "AB," + (TextPosX + index * width).ToString() + "," + TextPosY + "," + TextXMul + "," + TextYMul + "," + TextGap + "," + TextRotaion + "," + TextData + "\r\n";
            return cmd;
        }

        /// <summary>
        /// 打印二维码
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="mode"></param>
        /// <param name="type"></param>
        /// <param name="err"></param>
        /// <param name="mask"></param>
        /// <param name="mul"></param>
        /// <param name="rotatae"></param>
        /// <param name="data"></param>
        public void PrintQR(int x, int y, int mode, int type, string err, int mask, int mul, int rotatae, string data)
        {
            QRposX = x;
            QRposY = y;
            QRmode = mode;
            QRtype = type;
            QRerr = err;
            QRmask = mask;
            QRmul = mul;
            QRlength = data.Length;
            QRroatae = rotatae;
            QRdata = data;
            PrintQR(data);
        }

        /// <summary>
        /// 打印二维码
        /// </summary>
        /// <param name="data"></param>
        public void PrintQR(string data)
        {
            string mac = string.Empty;
            mac = data.Replace(":", "").Replace(".", "").Replace(" ", "");
            QRlength = mac.Length;
            QRdata = mac;
            string cmd = string.Empty;
            cmd += GetPrintConfigCmd();
            for (int i = 0; i < column; i++)
            {
                cmd += GetQRConfigCmd(i);
                if (isPrintText && mac.Length == 12)
                {
                    TextPosY = 31;
                    cmd += GetTextConfigCmd(mac.Substring(0, 6), i);
                    TextPosY = 57;
                    cmd += GetTextConfigCmd(mac.Substring(6, 6), i);
                }
            }

            cmd += "E";
            SendCommand(cmd);
        }

        /// <summary>
        /// 打印微信二维码
        /// </summary>
        /// <param name="data"></param>
        public void PrintWXQR(string data)
        {
            string mac = string.Empty;
            mac = data;
            QRlength = mac.Length;
            QRdata = mac;
            string cmd = string.Empty;
            cmd += GetPrintConfigCmd();
            for (int i = 0; i < column; i++)
            {
                cmd += GetQRConfigCmd(i);
            }

            cmd += "E";
            SendCommand(cmd);
        }
    }
}
